package cn.edu.scau.cmi.huangxiyan.client;

import java.util.Map;
import cn.edu.scau.cmi.huangxiyan.multipleton.Marshal;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MultipletonClient extends Application {
	private Map<String, Marshal> marshals = null;

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("MultipletonClient");
		VBox vBox = new VBox(15);
		vBox.setPadding(new Insets(15, 5, 5, 5));

		TextField textField = new TextField("请输入十大元帅之一的名称");

		TextArea textArea = new TextArea("");
		textArea.setPrefSize(600, 200);
		textArea.setEditable(false);

		Button button = new Button("创建");
		button.setPrefSize(200, 25);
		button.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				marshals = Marshal.addInstance(textField.getText());
				if (marshals.containsKey(textField.getText())) {
					textArea.appendText("名称：" + marshals.get(textField.getText()).getName() + " 对象："
							+ marshals.get(textField.getText()) + "\n");
				} else {
					textArea.appendText("十大元帅已满，不能再创建" + textField.getText() + "元帅！\n");
				}
			}
		});
		BorderPane borderPane = new BorderPane();
		borderPane.setCenter(button);

		vBox.getChildren().addAll(textField, textArea, borderPane);
		Scene scene = new Scene(vBox, 600, 300);
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}
